package com.itfetter.search.listen;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itfetter.api.client.ItemClient;
import com.itfetter.common.utils.BeanUtils;
import com.itfetter.search.domain.dto.ItemDTO;
import com.itfetter.search.domain.po.Item;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHost;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.tomcat.util.codec.binary.StringUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;


@RequiredArgsConstructor
@Component
@RocketMQMessageListener(topic = "rocket", consumerGroup = "canal-rocket-consumer")
public class CanalrocketMQListener implements RocketMQListener<String> {

    private RocketMQTemplate rocketMQTemplate;

    private final RestHighLevelClient esClient = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.74.128:9200")));


    @Override
    public void onMessage(String message) {
        System.out.println(message);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = null;
        try {
            s = objectMapper.readTree(message).get("type").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        updateAndInsert(message, s);

    }

    public void updateAndInsert(String message, String result) {
        ObjectMapper objectMapper = new ObjectMapper();
        String s = null;
        try {
            s = objectMapper.readTree(message).get("data").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ItemDTO itemDTO = JSONUtil.toBean(s.substring(1, s.length() - 1), ItemDTO.class);
         String doc = JSONUtil.toJsonStr(itemDTO);
        if (result.contains("UPDATE")) {
            System.out.println("数据库发生变化，修改");
            UpdateRequest request = new UpdateRequest("items", String.valueOf(itemDTO.getId()));
            request.doc(doc, XContentType.JSON);
            try {
                esClient.update(request, RequestOptions.DEFAULT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (result.contains("INSERT")) {
            System.out.println("数据发生变化，新增");
            IndexRequest request = new IndexRequest("items").id(String.valueOf(itemDTO.getId()));
            request.source(doc, XContentType.JSON);
            try {
                esClient.index(request, RequestOptions.DEFAULT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (result.contains("DELETE")) {
            System.out.println("数据库发生变化，删除，切记删除");
            DeleteRequest request = new DeleteRequest("items", String.valueOf(itemDTO.getId()));
            try {
                esClient.delete(request, RequestOptions.DEFAULT);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}